package cn.jiedh.app.meiweidk.mine;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.UI.AlertDialog;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.entity.ApplicationInfoBean;
import cn.jiedh.app.meiweidk.forgetpsw.ForgetPswActivity;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.login.LoginActivity;
import cn.jiedh.app.meiweidk.welcome.WelcomeActivity;

/**
 * @name：设置
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/20
 */

public class SettingActivity extends BaseActivity {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.customer_service_phone)
    TextView customerServicePhoneTv;
    private String customerServicePhone;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView(View view) {
        title.setText("设置");
        ApplicationInfoBean bean = SPUtils.getAppInfoFromSP();
        customerServicePhone = bean.getCustomerServicePhone();
        customerServicePhoneTv.setText(customerServicePhone);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.change_login_psw, R.id.forget_psw, R.id.about_us, R.id.consumer_hotline, R.id.logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.change_login_psw:
                ActivityUtil.next(this, ChangePswActivity.class);
                MobclickAgent.onEvent(activity, "my_setting_changepwd_click");
                SPUtils.saveBuried("click", "0", "设置-密码修改点击", "",
                        "我的-设置", "", "", "");
                break;
            case R.id.forget_psw:
                ActivityUtil.next(this, ForgetPswActivity.class);
                break;
            case R.id.about_us:
                MobclickAgent.onEvent(activity, "my_setting_about_click");
                ActivityUtil.next(this, AboutUsActivity.class);
                SPUtils.saveBuried("click", "0", "设置-关于我们点击", "",
                        "我的-设置", "", "", "");
                break;
            case R.id.consumer_hotline:
                getCall(customerServicePhone);
                break;
            case R.id.logout:
                new AlertDialog(this).builder().setMsg("是否要退出当前账号")
                        .setPositiveButton("确认", v -> {
                            SPUtils.clearUser(this);
                            ActivityUtil.next(this, LoginActivity.class);
                            finish();
                        }).setNegativeButton("取消", v -> {

                }).show();
                break;
            default:
                break;
        }
    }

    //拨打电话
    public void getCall(final String phone) {
        new AlertDialog(this).builder()
//                .setTitle("即将联系")
                .setMsg(phone)
                .setPositiveButton("呼叫", v -> {
                    // 检查是否获得了权限（Android6.0运行时权限）
                    if (ContextCompat.checkSelfPermission(SettingActivity.this,
                            Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        // 没有获得授权，申请授权
                        if (ActivityCompat.shouldShowRequestPermissionRationale(SettingActivity.this,
                                Manifest.permission.CALL_PHONE)) {
                            // 返回值：
//                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                            // 弹窗需要解释为何需要该权限，再次请求授权
                            Toast.makeText(SettingActivity.this, "请授权！", Toast.LENGTH_LONG).show();

                            // 帮跳转到该应用的设置界面，让用户手动授权
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
                        } else {
                            // 不需要解释为何需要该权限，直接请求授权
                            ActivityCompat.requestPermissions(SettingActivity.this,
                                    new String[]{Manifest.permission.CALL_PHONE}, 1);
                        }
                    } else {
                        // 已经获得授权，可以打电话
                        callPhone(phone);
                    }

                }).setNegativeButton("取消", v -> {

                }).show();
    }

    private void callPhone(String phone) {

        // 拨号：激活系统的拨号组件
        Intent intent = new Intent(); // 意图对象：动作 + 数据
        intent.setAction(Intent.ACTION_CALL); // 设置动作
        Uri data = Uri.parse("tel:" + phone); // 设置数据
        intent.setData(data);
        startActivity(intent); // 激活Activity组件

    }
}
